package com.air.controller;

import com.air.entity.Flight;
import com.air.service.FlightService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class FlightController {

    @Autowired
    FlightService flightService;

    @RequestMapping(value = "/flight",method = RequestMethod.GET)
    @ResponseBody
    public String selecFlight(@RequestParam("takeoff") String takeoff, @RequestParam("landing") String landing, @RequestParam("firstdate") String firstdate){
        System.out.println("接收到的"+takeoff + " "+landing + " "+ firstdate);
        List<Flight> flights = flightService.selecFlight(takeoff, landing, firstdate);
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        System.out.println(gson.toJson(flights));
        return gson.toJson(flights);
    }

    @RequestMapping(value = "/flightById",method = RequestMethod.GET)
    @ResponseBody
    public String selecFlightById(@RequestParam("id") String id){
        Flight flights = flightService.selecFlightById(id);
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        System.out.println(gson.toJson(flights));
        return gson.toJson(flights);
    }

}
